package com.cetc39.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;

import com.cetc39.bean.Department;
import com.cetc39.bean.FirstPlan;
import com.cetc39.bean.Project;
import com.cetc39.bean.Role;
import com.cetc39.service.ClientService;
import com.cetc39.service.DeptService;
import com.cetc39.service.FirstplanService;
import com.cetc39.service.NodeService;
import com.cetc39.service.ProjectFieldService;
import com.cetc39.service.ProjectService;
import com.cetc39.service.UserService;
import com.cetc39.util.DateJsonValueProcessor;
import com.cetc39.util.JsonValueProcessorImplTest;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FirstplanAction extends ActionSupport {

	private ProjectService projectService;
	private FirstplanService firstplanService;
	private NodeService nodeService;
	private UserService userService;
	private DeptService deptService;
	private ProjectFieldService projectFieldService;
	private ClientService clientService;
	private List<?> projectList;
	private FirstPlan firstplan;
	// private Date plan_setstart;
	// private Date plan_setfinish;
	// private Date plan_lastedit;
	// private Date plan_issue;
	private String setStart;
	private int id;
	private List<?> nodeList;
	private List<?> planList;
	private String opcode;
	private int project_id;
	private FirstPlan pre_firstplan = new FirstPlan();
	private int node_id;
	private int plan_id;
	private List<?> releasedPlanList;
	
	private String leader_39;
	private String dept;
	private String field;
	private String start_date;
	private String status;
	private String contract_partya;
	private String project_code;
	private List<?> leaderList;
	private List<?> deptList;
	private List<?> fieldList;
	private List<?> clientList;
	private String role_id;
	private String result;
	private String user_id;
	private List<?> nodeListWithoutSort;
	private String version;

	public String preCreateFirstplan() {
		System.out.println("preCreateFirstplan(" + role_id + user_id + ")");
		projectList = projectService.preCreateFirstplan(Integer.parseInt(role_id), Integer.parseInt(user_id));
		
		return SUCCESS;
	}
	
	public String firstplanVersionChange() {
		FirstPlan plan = firstplanService.findById(id);
		String v = plan.getPlan_version();
		String front = v.split("\\.")[0];
		String after = v.split("\\.")[1];
		if("dad".equals(version)) {
			v = Integer.toString(Integer.parseInt(front) + 1);
			v = v + ".0";
		} else if("son".equals(version)) {
			after = Integer.toString(Integer.parseInt(after) + 1);
			v = front + "." + after;
		}
		plan.setPlan_version(v);
		firstplanService.update(plan);
		return SUCCESS;
	}

	public String createFirstplanCommit() throws Exception {
		System.out.println(project_id);
		
		Project p = projectService.findById(project_id);
		
		List<?> l = projectService.findHasNoFirstplan();
		
		boolean flag = false;
		for(int i = 0; i < l.size(); i++) {
			Project j = (Project) l.get(i);
			if(j.getId() == p.getId()) {
				flag = true;
			}
		}
		if(flag) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmm");
			Date plan_setStart = sdf.parse(setStart);
			pre_firstplan.setPlan_setstart(plan_setStart);
			pre_firstplan.setPlan_version("1.0");
			pre_firstplan.setPlan_id(sdf2.format(new Date()));
			pre_firstplan.setProject(p);
			
			firstplanService.add(pre_firstplan);
			id = pre_firstplan.getId();
		} else {
			List<?> fp_list = firstplanService.findByProjectId(project_id);
			FirstPlan fp = (FirstPlan) fp_list.get(0);
			id = fp.getId();
		}
		
		return SUCCESS;
	}

	public String findAllPlan() {
		planList = firstplanService.findAll();
		if("firstplan_check".equals(opcode)) {
			return "firstplan_check";
		} else if("firstplan_update".equals(opcode)) {
			return "firstplan_update";
		} else {
			return null;
		}
	}
	
	public String findReleasedPlan() {
		releasedPlanList = firstplanService.findReleasedPlan();
		Role r = new Role();
		r.setId(1);
		leaderList = userService.findByRole(r);
		deptList = deptService.findAllDept();
		fieldList = projectFieldService.findAll();
		clientList = clientService.findAll();
		
		if("firstplan_check".equals(opcode)) {
			return "firstplan_check";
		} else if("firstplan_update".equals(opcode)) {
			return "firstplan_update";
		}
		return null;
	}
	
	public String findReleasedPlanByCondition() {
		String conditions[] = new String[7];
		conditions[0] = leader_39;
		conditions[1] = dept;
		conditions[2] = field;
		conditions[3] = status;
		conditions[4] = start_date;
		conditions[5] = contract_partya;
		conditions[6] = project_code;
		
		Role r = new Role();
		r.setId(1);
		leaderList = userService.findByRole(r);
		deptList = deptService.findAllDept();
		fieldList = projectFieldService.findAll();
		clientList = clientService.findAll();
		
		releasedPlanList = firstplanService.findReleasedPlanByCondition(conditions);
		
		if("check".equals(opcode)) {
			return "check";
		} else if("update".equals(opcode)) {
			return "update";
		} else {
			return SUCCESS;
		}
		
	}


	
	public String findPlanById() {
		firstplan = firstplanService.findById(id);
		nodeList = nodeService.findByFirstplanId(id);
		nodeListWithoutSort = nodeService.findByFirstplanIdWithoutSort(id);
		if("forUpdate".equals(opcode)) {
			return "forUpdate";
		} else if("forDetail".equals(opcode)) {
			return "forDetail";
		} else {
			return SUCCESS;
		}
	}
	
	public String release_plan() {
		firstplanService.release(plan_id);
		Date release_date = new Date();
		FirstPlan releasedPlan = firstplanService.findById(plan_id);
		releasedPlan.setPlan_issue(release_date);
		firstplanService.update(releasedPlan);
		
		opcode="firstplan_check";
		return SUCCESS;
	}
	
	public String access_plan() {
		List<?> firstplanList = firstplanService.findByProjectId(project_id);
		firstplan = (FirstPlan) firstplanList.get(0);
		nodeList = nodeService.findByFirstplanId(firstplan.getId());
		return SUCCESS;
	}
	
	public String accessOrNot() {
		if("1".equals(result)) {
			firstplanService.access_plan(plan_id);
		} else {
			firstplanService.reject_plan(plan_id);
		}
		opcode = "firstplan_check";
		return SUCCESS;
	}
	
	public String deleteNode() {
		nodeService.delete(node_id);
		return SUCCESS;
	}

	public String findPlanByIdJSON() throws IOException {
		firstplan = firstplanService.findById(id);
		int projectId = firstplan.getProject().getId();
		Project p = projectService.findById(projectId);
		firstplan.setProject(p);
		nodeList = nodeService.findByFirstplanId(id);
		JSONObject obj = JSONObject.fromObject(firstplan);
		String jsonStr = obj.toString();
		
		JSONArray arr = JSONArray.fromObject(nodeList);
		String jsonString = arr.toString(); 
		System.out.println(jsonString);
		HttpServletResponse response = (HttpServletResponse) ActionContext
				.getContext().get(
						org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(jsonStr + "|" + jsonString);
		return null;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public List<?> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<?> projectList) {
		this.projectList = projectList;
	}

	public FirstPlan getFirstplan() {
		return firstplan;
	}

	public void setFirstplan(FirstPlan firstplan) {
		this.firstplan = firstplan;
	}

	public String getSetStart() {
		return setStart;
	}

	public void setSetStart(String setStart) {
		this.setStart = setStart;
	}

	public void setFirstplanService(FirstplanService firstplanService) {
		this.firstplanService = firstplanService;
	}

	public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}

	public List<?> getNodeList() {
		return nodeList;
	}

	public void setNodeList(List<?> nodeList) {
		this.nodeList = nodeList;
	}

	public List<?> getPlanList() {
		return planList;
	}

	public void setPlanList(List<?> planList) {
		this.planList = planList;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getOpcode() {
		return opcode;
	}

	public void setOpcode(String opcode) {
		this.opcode = opcode;
	}


	public int getProject_id() {
		return project_id;
	}


	public void setProject_id(int project_id) {
		this.project_id = project_id;
	}


	public FirstPlan getPre_firstplan() {
		return pre_firstplan;
	}


	public void setPre_firstplan(FirstPlan pre_firstplan) {
		this.pre_firstplan = pre_firstplan;
	}


	public int getNode_id() {
		return node_id;
	}


	public void setNode_id(int node_id) {
		this.node_id = node_id;
	}


	public int getPlan_id() {
		return plan_id;
	}


	public void setPlan_id(int plan_id) {
		this.plan_id = plan_id;
	}


	public List<?> getReleasedPlanList() {
		return releasedPlanList;
	}


	public void setReleasedPlanList(List<?> releasedPlanList) {
		this.releasedPlanList = releasedPlanList;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}


	public void setProjectFieldService(ProjectFieldService projectFieldService) {
		this.projectFieldService = projectFieldService;
	}


	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}


	public List<?> getLeaderList() {
		return leaderList;
	}


	public void setLeaderList(List<?> leaderList) {
		this.leaderList = leaderList;
	}


	public List<?> getDeptList() {
		return deptList;
	}


	public void setDeptList(List<?> deptList) {
		this.deptList = deptList;
	}


	public List<?> getFieldList() {
		return fieldList;
	}


	public void setFieldList(List<?> fieldList) {
		this.fieldList = fieldList;
	}


	public List<?> getClientList() {
		return clientList;
	}


	public void setClientList(List<?> clientList) {
		this.clientList = clientList;
	}


	public String getLeader_39() {
		return leader_39;
	}


	public void setLeader_39(String leader_39) {
		this.leader_39 = leader_39;
	}


	public String getDept() {
		return dept;
	}


	public void setDept(String dept) {
		this.dept = dept;
	}


	public String getField() {
		return field;
	}


	public void setField(String field) {
		this.field = field;
	}


	public String getStatus() {
		return status;
	}


	public void setStatus(String status) {
		this.status = status;
	}


	public String getContract_partya() {
		return contract_partya;
	}


	public void setContract_partya(String contract_partya) {
		this.contract_partya = contract_partya;
	}


	public String getProject_code() {
		return project_code;
	}


	public void setProject_code(String project_code) {
		this.project_code = project_code;
	}


	public String getRole_id() {
		return role_id;
	}


	public void setRole_id(String role_id) {
		this.role_id = role_id;
	}


	public String getResult() {
		return result;
	}


	public void setResult(String result) {
		this.result = result;
	}


	public String getStart_date() {
		return start_date;
	}


	public void setStart_date(String start_date) {
		this.start_date = start_date;
	}


	public String getUser_id() {
		return user_id;
	}


	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}


	public List<?> getNodeListWithoutSort() {
		return nodeListWithoutSort;
	}


	public void setNodeListWithoutSort(List<?> nodeListWithoutSort) {
		this.nodeListWithoutSort = nodeListWithoutSort;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}


	

}
